﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTOSingTowerMall;
using FacadeSingTowerMall;
using System.IO;

namespace WebFormSingTowerMall
{
    public partial class BlockDetails : System.Web.UI.Page
    {
        BlockFacade bf = new BlockFacade();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            
                if (!IsPostBack)
                {
                    DropDownList1.Items.Clear();
                    DropDownList1.Items.Add("Select");
                    foreach (ILevel lvl in bf.getLevels())
                        DropDownList1.Items.Add(lvl.Level1.ToString());

                    dboxBlockType.Items.Clear();
                    dboxBlockType.Items.Add("Select");
                    foreach (ICategory ct in bf.getCategories())
                        dboxBlockType.Items.Add(ct.CategoryName);
                
                if (!string.IsNullOrEmpty(Request.QueryString["levelid"]))
                {
                    DropDownList1.SelectedValue = Request.QueryString["levelid"];
                    IBlock blk = bf.getBlockByID(Convert.ToInt32(Request.QueryString["blockid"]));
                    ICategory cat = bf.getCategoryByID(blk.Type);
                    txtBlockID.Text = blk.BlockID.ToString();
                    txtDimension.Text = blk.Dimension;
                    txtLocation.Text = blk.Location;
                    dboxBlockType.SelectedValue = cat.CategoryName;
                    ILevel level = bf.getLevelByID(Convert.ToInt32(DropDownList1.SelectedValue));
                    if (!string.IsNullOrEmpty(level.MapImage))
                        ImageMap1.ImageUrl = level.MapImage;
                    else
                        ImageMap1.ImageUrl = "~/Images/NoImage.jpg";
                    List<IBlock> blockList = bf.getBlocksByLevel(Convert.ToInt32(DropDownList1.SelectedValue)).ToList();
                    List<int> blockIds = new List<int>();
                    foreach (IBlock b in blockList)
                    {
                        if (!blockIds.Contains(b.BlockID))
                            blockIds.Add(b.BlockID);
                    }
                    Table1.Rows.Clear();
                    TableRow r = new TableRow();
                    int remaining = blockIds.Count;
                    while (remaining > 0)
                    {
                        TableRow tr = new TableRow();
                        int count = 0;
                        for (int i = (remaining - 1); count < 15 && i >= 0; i--)
                        {
                            TableCell tc = new TableCell();
                            tc.Height = 50;
                            tc.Width = 50;
                            // Add the control to the TableCell
                            HyperLink hlink = new HyperLink();
                            hlink.CssClass = "box";
                            hlink.Text = blockIds[i].ToString();
                            hlink.ID = blockIds[i].ToString();
                            hlink.NavigateUrl = "/BlockDetails.aspx?levelid=" + DropDownList1.SelectedValue + "&blockid=" + hlink.Text;
                            tc.Controls.Add(hlink);
                            // Add the TableCell to the TableRow
                            tr.Cells.Add(tc);
                            remaining--;
                            count++;
                        }
                        // Add the TableRow to the Table
                        Table1.Rows.Add(tr);
                    }
                }
        }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue != "Select")
            {
                ILevel level = bf.getLevelByID(Convert.ToInt32(DropDownList1.SelectedValue));
                if (!string.IsNullOrEmpty(level.MapImage))
                    ImageMap1.ImageUrl = level.MapImage;
                else
                    ImageMap1.ImageUrl = "~/Images/NoImage.jpg";
                List<IBlock> blockList = bf.getBlocksByLevel(Convert.ToInt32(DropDownList1.SelectedValue)).ToList();
                List<int> blockIds = new List<int>();
                foreach (IBlock b in blockList)
                {
                    if (!blockIds.Contains(b.BlockID))
                        blockIds.Add(b.BlockID);
                }
                Table1.Rows.Clear();
                TableRow r = new TableRow();
                int remaining = blockIds.Count;
                while (remaining > 0)
                {
                    TableRow tr = new TableRow();
                    int count =0;
                    for (int i = (remaining-1); count<15 && i >= 0; i--)
                    {
                        TableCell tc = new TableCell();
                        tc.Height = 50;
                        tc.Width = 50;
                        // Add the control to the TableCell
                        HyperLink hlink = new HyperLink();
                        hlink.CssClass = "box";
                        hlink.Text = blockIds[i].ToString();
                        hlink.ID = blockIds[i].ToString();
                        hlink.NavigateUrl = "/BlockDetails.aspx?levelid=" + DropDownList1.SelectedValue +"&blockid=" + hlink.Text;
                        tc.Controls.Add(hlink);
                        // Add the TableCell to the TableRow
                        tr.Cells.Add(tc);
                        remaining--;
                        count++;
                    }
                    // Add the TableRow to the Table
                    Table1.Rows.Add(tr);
                }

                txtBlockID.Text = "";
                txtDimension.Text = "";
                txtLocation.Text = "";
                dboxBlockType.SelectedValue = "Select";
            }
        }



        protected void Button2_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                lblStatusMessage.Text = bf.AddBlock(Convert.ToInt32(txtBlockID.Text), dboxBlockType.SelectedValue, txtLocation.Text, txtDimension.Text);

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if
            (FileUpload1.HasFile)
            {
                try
                {
                    if
                    (FileUpload1.PostedFile.ContentType =="image/jpeg")
                    {
                        if
                        (FileUpload1.PostedFile.ContentLength < 1028000)
                        {
                            string
                            filename = Path.GetFileName(FileUpload1.FileName);
                            if (DropDownList1.SelectedValue != "Select")
                            {
                                FileUpload1.SaveAs(Server.MapPath("~/Maps/") + DropDownList1.SelectedValue + ".jpg");
                                lblStatusMessage.Text = "File uploaded successfully!";
                                ImageMap1.ImageUrl = "~/Maps/" + DropDownList1.SelectedValue + ".jpg";
                                bf.ModifyImage(Convert.ToInt32(DropDownList1.SelectedValue), "~/Maps/" + DropDownList1.SelectedValue + ".jpg");
                            }
                            else
                                lblStatusMessage.Text = "Please select the level";
                        }
                        else

                            lblStatusMessage.Text =
                            "File maximum size is 1 MB";
                    }
                    else

                        lblStatusMessage.Text =
                        "Only JPEG files are accepted!";
                }
                catch
                (Exception
                exc)
                {
                    lblStatusMessage.Text =
                    "The file could not be uploaded. The following error occured: "

                    + exc.Message;
                }
            }
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (dboxBlockType.SelectedValue == "Select")
            {
                args.IsValid = false; 
            }
        }

        protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (DropDownList1.SelectedValue == "Select")
            {
                args.IsValid = false;
            }
        }
    }
}